<?php

class Idea_home extends Controller{

	function idea_home()
	{
			parent::Controller();
      		$this->load->model(array("caf_model","user_model"));
			$this->load->helper(array('form','url'));
			$this->load->library(array('session'));
				
	}
  
  function general_dashboard()
  {
    //$this->validatesession->CI_validatesession();
    $user_ses = $this->session->userdata('userlogindetails');
    if($user_ses['user_id'] && ($user_ses['user_group'] == 2))
    {
        $displayform ='';
        $displayform .='<div id=dashboard ><span>Dashboard</span> <img align=right src="'.base_url().'images/home.png" alt="home" /><img align=right src="'.base_url().'images/back.png" alt="back" /></div>';
        $displayform .= '<span style="float:left;">Welcome '.$user_ses['user_name'].'</span><div id=logout><a href="'.base_url().'idea_home/logout">Logout</a></div>';
        $displayform .= '<div id=space>';
        
        $displayform .='<div id=menu><fieldset><legend>My Home</legend>';
        
        $displayform .= '<div class=dashbd_img><a href="'.base_url().'caf/select_batchcard">Data Entry</a></div>';
        $displayform .= '<div class=dashbd_img><a href="'.base_url().'qc_caf/select_batchcardqc">QC Entry</a></div>';
        $displayform .= '<div class=dashbd_img><a href="'.base_url().'idea_home/mystatus">My Status</a></div>';
        
        $displayform .= '<div class=dashbd_img><a href="'.base_url().'idea_home/change_pwd">Change Password</a></div>';
        
        
        $displayform .='</fieldset></div>';
        $displayform .='</div>';
        $data = array('formdisplay'=>$displayform ); 
        /* 
         $this->template->add_js('$(document).ready(function(){
                
          window.history.forward();
        });
              ','embed');
         * 
         * 
         */ 
        $this->template->write_view('header','header', $data, True);
        $this->template->write_view('content', 'general');
        $this->template->render();
        }
        else
        {
          echo "Forbidden to Access!!";
        }
    
  }
  
	function admin_dashboard()
	{
	   $user_ses = $this->session->userdata('userlogindetails');
    if($user_ses['user_id'] && ($user_ses['user_group'] == 1))
    { 
    		$displayform ='';
    		$displayform .='<div id=dashboard ><span>Dashboard</span> <img align=right src="'.base_url().'images/home.png" alt="home" /><img align=right src="'.base_url().'images/back.png" alt="back" /></div>';
            $displayform .= '<span style="float:left;">Welcome '.$user_ses['user_name'].'</span><div style="float:right;"><a href="'.base_url().'idea_home/change_pwd">Change Password</a>&nbsp;<a href="'.base_url().'add_xls">Data Import</a>&nbsp;<a href="'.base_url().'data_export/show_menu">Data Export</a>&nbsp;<a href="'.base_url().'idea_home/show_users">Show Users</a>&nbsp;<div id=logout><a href="'.base_url().'idea_home/logout">Logout</a></div></div>';
    		$displayform .= '<div id=space>';
    		$displayform .='<div id=status>';
    		$displayform .='<fieldset><legend>DP 24hrs Status</legend>';
    		$displayform .='</fieldset></div>';
    		$displayform .='<div id=menu><fieldset><legend>Manage Application </legend>';
    		
    		$displayform .= '<div class=dashbd_img><a href='.base_url().'states/list_states "><img src="'.base_url().'images/states.png" alt=state /></a></div>';
    		$displayform .= '<div class=dashbd_img><a href='.base_url().'cities/list_cities "><img src="'.base_url().'images/cities.png" alt=city /></a></div>';
    		$displayform .= '<div class=dashbd_img><a href='.base_url().'areas/list_areas "><img src="'.base_url().'images/areas.png" alt=area /></a></div>';
    		
    		$displayform .= '<div class=dashbd_img><a href='.base_url().'poi/list_poi "><img src="'.base_url().'images/poi.png" alt=poi /></a></div>';
    		$displayform .= '<div class=dashbd_img><a href='.base_url().'task/list_task ><img src="'.base_url().'images/task.png" alt=task /></a></div>';
    		$displayform .= '<div class=dashbd_img><a href='.base_url().'user/list_users "><img src="'.base_url().'images/users.png" alt=users /></a></div>';
    		
    		$displayform .='</fieldset></div>';
    		$displayform .='</div>';
    		$data = array('formdisplay'=>$displayform );
    		/*
    		 $this->template->add_js('$(document).ready(function(){
                
          window.history.forward();
        });
              ','embed');
         * 
         */	
    		$this->template->write_view('header','header', $data, True);
    		$this->template->write_view('content', 'general');
    		$this->template->render();
    		}
      else
      {
          echo "Forbidden to access!!!";
      }
        
	}
		
  function mystatus()
  {
    $user_ses = $this->session->userdata('userlogindetails');
     
    if($user_ses['user_id'] && ($user_ses['user_group'] == 2))
    {
        $displayform = '';
      
        $info = $this->caf_model->get_userinfo(0,0);
     //   $user = $info->result_array();
        $displayform .= '<div id=dashboard ><span>My Status</span>&nbsp;<a href='.base_url().'/idea_home/general_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" id="back"" alt="back" ONCLICK="history.go(-1)" /></div>';
        
        $displayform .= '<span style="float:left;">Welcome '.$user_ses['user_name'].'</span><div id=logout><a href="'.base_url().'idea_home/logout">Logout</a></div>';
        $displayform .= '<br/>';
        $displayform .= '<div id=mystatus>';
        $displayform .= '<fieldset id="daily">';
        $displayform .= '<legend>'.date('d-m-Y').'</legend>';
        $displayform .= "<div> <label>Data Entry:</label>";
        //print_r($info);exit;
     
        $displayform .= $info[0];
         
        $displayform .= "</div>";
        $displayform .= "<div> <label>Validation:</label>";
        $displayform .= $info[1];
        $displayform .= "</div>";
        
        $displayform .= '</fieldset>';
        
        $displayform .= '<fieldset id="all_report" >';
        $displayform .= '<legend>All Reports</legend>';
        
        $displayform .= 'Start Date:';
        $displayform .= '<input type="text" class="date_picker" id="start_date" size=7 />&nbsp;';
        
        $displayform .= 'End Date:';
        $displayform .= '<input type="text" class="date_picker" id="end_date" size=7 />&nbsp;';
        $displayform .= '<input type="button" id="get_val" value="Search" />'; 
     
        $displayform .= '<div id="display_result"></div>';
        
        
        $displayform .= '</fieldset>';
        
        
        $displayform .= '</div>';
        
        
        $data = array('formdisplay'=>$displayform );  

        $this->template->add_js('js/ui/jquery.ui.datepicker.js');
        $this->template->add_js('
        $(document).ready(function(){
                $(function() {
                           $( ".date_picker" ).datepicker({dateFormat: "dd/mm/yy"});
                           
                        
                      });
                      
                 $("#get_val").click(function()
                 {
                      var start = $("#start_date").val();
                      var end = $("#end_date").val();
                      
                      $.post("'.base_url().'idea_home/get_mydetails", { start_date: start, end_date: end },
                          function(data){
                                    $("#display_result").html("<br/>"+"<label>Total Data Entry:</label>"+data.entry+"<br/>"+
                                    "<label>Total Validation:</label>"+data.validation);
                                    }, "json");                      
                      
                 });     
                });
                
                
                
        ','embed');
        //$this->template->add_css('css/style.css');
        $this->template->write_view('header','header', $data, True);
        $this->template->write_view('content', 'other_general');
        $this->template->render();
  }
  else
      {
         echo "Forbidden to access!!!";
      }
  }

  function show_users()
  {
    $user_ses = $this->session->userdata('userlogindetails');
     
    if($user_ses['user_id'] && ($user_ses['user_group'] == 1))
    {
      $this->load->library('check_users');
	  $arr = $this->check_users->Check_session();
	  print_r($arr);//exit;
	  
      $displayform = '';
      $displayform .='<div id=dashboard ><span>Currently Logged Users</span> &nbsp;<a href='.base_url().'/idea_home/admin_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id="back"" alt="back" ONCLICK="history.go(-1)" /></div>';
       $displayform .= '<span style="float:left;">Welcome '.$user_ses['user_name'].'</span><div id=logout><a href="'.base_url().'idea_home/logout">Logout</a></div>';
      $displayform .= $this->check_users->Check_session();
      
        $data = array('formdisplay'=>$displayform );
          
        $this->template->write_view('header','header', $data, True);
        $this->template->write_view('content', 'other_general');
        $this->template->render();
    }
    else
    {
      echo "Forbidden to access";
    }
      
  }
  
  function get_mydetails()
  {
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];
    list($d,$m,$y) = explode('/',$start_date);
    $cur_start = $y.'-'.$m.'-'.$d;
  //  echo $cur_start;
    list($d,$m,$y) = explode('/',$end_date);
    $cur_end = $y.'-'.$m.'-'.$d;
    //echo $cur_end;
    
     $info = $this->caf_model->get_userinfo($cur_start,$cur_end);
     
    
     $all_det = array('entry'=>$info[0],'validation'=>$info[1]);
     echo json_encode($all_det);
     
  }
  
  function logout()
  {
    $this->session->unset_userdata('userlogindetails');
   // $this->session->sess_destroy();
   // redirect("login");
    
    $displayform = '';
    $displayform .= '<div align=left>Please Wait...</div>';
    
     $data = array('formdisplay'=>$displayform ); 
      
     $this->template->add_js('$(document).ready(function(){
                
          window.history.forward();
         
          window.location = "'.base_url().'"
          });            
          
         
            
              ','embed');
              
      $this->template->write_view('header','header', $data, True);
      $this->template->write_view('content', 'general');
      $this->template->render();
     
     redirect("login");
   
   }
  
 
    
  
  function change_pwd()
  {
  	
    $user_ses = $this->session->userdata('userlogindetails');
     
    if($user_ses['user_id'])
    {
    	 $displayform = '';
    	 $displayform .= '<div id=dashboard ><span>Change Password</span>&nbsp;<img align=right src="'.base_url().'images/home.png" style="cursor:pointer;" alt="home" ONCLICK="history.go(-1)" /><img align=right src="'.base_url().'images/back.png" id="back"" alt="back" ONCLICK="history.go(-1)" /></div>';
        
        $displayform .= '<span style="float:left;">Welcome '.$user_ses['user_name'].'</span><div id=logout><a href="'.base_url().'idea_home/logout">Logout</a></div>';
       
       	$displayform .= '<br/>';
		
		$displayform .= '<form action='.base_url().'idea_home/save_change_pwd method=post >';
		$displayform .='<table  >';
		$displayform .='<tr>';
		$displayform .='<td align=right>';
		$displayform .= 'New Password:';
		$displayform .='</td>';
		$displayform .='<td align=left>';
		$displayform .= '<input type=password name=pwd id=pwd />';
		$displayform .='</td>';
		$displayform .='</tr>';
		
		$displayform .='<tr>';
		$displayform .='<td align=right>';
		$displayform .= 'Retype Password:';		
		$displayform .='</td>';
		$displayform .='<td align=left>';
		$displayform .= '<input type=password name=repwd id=repwd />';
		$displayform .='</td>';
		$displayform .='</tr>';
			$displayform .='<tr>';
		$displayform .='<td colspan=2 align=center >';
		$displayform .= '<input type="submit" id=sub value="Change Password" />';
		$displayform .='</td>';
		$displayform .='</tr>';
		
		$displayform .='</table>';
		$displayform .='</form>';
		$data = array('formdisplay'=>$displayform );  
		 $this->template->add_js('$(document).ready(function(){
		 	
			$("#sub").click(function(){
				var pwd = $("#pwd").val(),
				repwd = $("#repwd").val();
				if(pwd.length <=5)
				{
					alert("please enter password of length minimum 6");
					return false;
				}
				if(pwd =="")
				
				return false;
				
				if(pwd == repwd)
				{
					return true;	
				}	
				else
				{
					alert("Password didnt match,please try again");
					$("#pwd").focus();
					return false;
				}
			});
			
		 });','embed');
		 $this->template->write_view('header','header', $data, True);
	     $this->template->write_view('content', 'general');
	     $this->template->render();
		
    }
    else
    {
      echo "forbidden to access";
    }
  }

	function save_change_pwd()
	{
		$this->user_model->change_pwd();	
		 $user_ses = $this->session->userdata('userlogindetails');
     
    if($user_ses['user_group']==2)
    {
		redirect('idea_home/general_dashboard');
	}	
	else
	{
		redirect('idea_home/admin_dashboard');
	}
	
	}
	
}
